//
//  SDConsultController.m
//  SuperDoctor
//
//  Created by 陶宏路 on 16/7/7.
//  Copyright © 2016年 SD. All rights reserved.
//

#define TEXTCOLOR [UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1]
#import "SDConsultController.h"

@interface SDConsultController ()<UITextViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITextView * textView;
@property (nonatomic,strong) UIButton * rightButton;
@property (nonatomic,strong) UILabel * textLable;
@property (nonatomic,strong) UILabel * numLable;
@property (nonatomic,strong) UITableView * consultTable;

@end

@implementation SDConsultController

static int  num = 200;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self setUpLayout];
}

-(UITableView *)consultTable{
    
    if (_consultTable == nil) {
        
        _consultTable = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.view addSubview:_consultTable];
        _consultTable.delegate = self;
        _consultTable.dataSource = self;
        _consultTable.scrollEnabled = NO;
        _consultTable.userInteractionEnabled = NO;
        _consultTable.tableFooterView = [[UIView alloc]init];

    }
    return _consultTable;
}

-(void)setUpUI{
    
    UITextView * textView = [[UITextView alloc]init];
    self.textView = textView;
    [self.view addSubview:textView];
    textView.backgroundColor =TEXTCOLOR;
    self.automaticallyAdjustsScrollViewInsets = false;
    self.textView.font = [UIFont systemFontOfSize:18];
    self.textView.keyboardType = UIKeyboardTypeDefault;
    self.textView.delegate = self;
    
    UIButton * rightButton = [[UIButton alloc]init];
    self.rightButton = rightButton;
    [rightButton setTitle:@"提交反馈" forState:UIControlStateNormal];
    [self.view addSubview:rightButton];
    self.rightButton.backgroundColor = kThemeColor;
    self.rightButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
    
    UILabel * textLable = [[UILabel alloc]init];
    self.textLable = textLable;
    [self.view addSubview:textLable];
    self.textLable.text = [NSString stringWithFormat:@"%d",num];
    
    UILabel * numLable = [[UILabel alloc]init];
    self.numLable = numLable;
    [self.view addSubview:numLable];
    self.numLable.text = @"你还可以输入的字数为:";
}

// 235 235 241
-(void)setUpLayout{
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(70);
        make.left.mas_equalTo(self.view.mas_left).offset(5);
        make.right.mas_equalTo(self.view.mas_right).offset(-5);
        make.height.mas_equalTo(200);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.textView.mas_bottom).offset(5);
        make.right.mas_equalTo(self.view.mas_right).offset(-5);
        make.width.mas_equalTo(90);
    }];
    
    [self.textLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.rightButton.mas_centerY);
        make.right.mas_equalTo(self.rightButton.mas_left).offset(-10);
        make.width.mas_equalTo(50);
    }];
    
    [self.numLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.textLable.mas_centerY);
        make.right.mas_equalTo(self.textLable.mas_left).offset(-10);
    }];
    
    [self.consultTable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.rightButton.mas_bottom).offset(5);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];

}

// 代理方法
-(void)textViewDidChange:(UITextView *)textView{

    NSString * text = textView.text;
    
    self.textLable.text = [NSString stringWithFormat:@"%ld",num - text.length];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location>=200)
    {
        //控制输入文本的长度
        return  NO;
    }
    if ([text isEqualToString:@"\n"]) {
        //禁止输入换行
        return NO;
    }
    else
    {
        return YES;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"consultCell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"consultCell"];
        
        if (indexPath.row == 0) {
            
            cell.imageView.image = [UIImage imageNamed:@"dianhua"];
            cell.textLabel.text = @"客服电话: 400-616-6113";
        }else{
            
            cell.imageView.image = [UIImage imageNamed:@"qq"];
            cell.textLabel.text = @"用户交流群: QQ464768569";
        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    

    return 50;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UILabel * lable = [[UILabel alloc]init];
    lable.backgroundColor = TEXTCOLOR;
    lable.text = @"    欢迎联系我们";
    
    return lable;
}

@end
